package monopoly.frames;

import static java.lang.String.format;

import java.awt.AWTEvent;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JLayer;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.LayerUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

@SuppressWarnings("serial")
public class FrameLog extends TransparentJInternalFrame {

    public FrameLog (JDesktopPane desktop){
        //setFrameIcon(imageIcon);
        setTitle("Log");
        setLocation(desktop.getWidth() / 6, desktop.getHeight() / 4);
        setSize((int) (desktop.getWidth() / 1.5), desktop.getHeight() / 2);
        setMaximizable(true);
        setResizable(true);
        setIconifiable(true);
        setTransparentColor(new Color(0, 0, 0, 127));
        getContentPane().add(new JScrollPane(new JLayer<>(new JTextPane(), new LayerUI<JTextPane>() {

            private int mX = Integer.MIN_VALUE;
            private int mY = Integer.MIN_VALUE;
            private final float radius = 90.0F;
            private final float[] dist = {0.0F, 1.0F};
            private final Color[] colors = {new Color(0.0F, 0.0F, 0.0F, 0.0F), Color.BLACK};
            private final AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6F);
            private final Point2D center = new Point2D.Double(mX, mY);
            public void installUI(JComponent c) {
                super.installUI(c);
                ((JLayer<?>) c).setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
            }

            public void uninstallUI(JComponent c) {
                ((JLayer<?>) c).setLayerEventMask(0L);
                super.uninstallUI(c);
            }

            public void paint(Graphics g, JComponent c) {
                final Graphics2D g2 = (Graphics2D) g.create();
                super.paint(g2, c);
                center.setLocation(mX, mY);
                g2.setPaint(new RadialGradientPaint(center, radius, dist, colors));
                g2.setComposite(alphaComposite);
                g2.fillRect(0, 0, c.getWidth(), c.getHeight());
                g2.dispose();
            }

            protected void processMouseMotionEvent(MouseEvent e, JLayer l) {
                final Point p = SwingUtilities.convertPoint(e.getComponent(), e.getX(), e.getY(), l);
                mX = p.x;
                mY = p.y;
                l.repaint();
            }
        })), BorderLayout.CENTER);
        setVisible(true);
    }
    
//    private static synchronized void imprimir(String s) {
//        imprimir(s, (SimpleAttributeSet) null);
//    }
//
//    private static synchronized void imprimir(String s, SimpleAttributeSet sas) {
//        hoy.setTime(System.currentTimeMillis());
//        try {
//            doc.insertString(doc.getLength(), format("%s - %s\n", sdf.format(hoy), s), sas);
//            Log.setCaretPosition(doc.getLength());
//        } catch (BadLocationException ignorada) {
//        }
//    }
//
//    private static synchronized void imprimir(String s, Throwable t) {
//        imprimir(s, styleException, t);
//    }
//
//    private static synchronized void imprimir(String s, SimpleAttributeSet sas, Throwable t) {
//        imprimir(s, sas);
//        imprimir(t);
//    }
//
//    private static synchronized void imprimir(Throwable t) {
//        try {
//            doc.insertString(doc.getLength(), t.toString() + '\n', styleException);
//            for (StackTraceElement stackTraceElement : t.getStackTrace()) {
//                doc.insertString(doc.getLength(), format("\tat %s\n", stackTraceElement), styleException);
//            }
//            final Throwable cause = t.getCause();
//            if (cause != null) {
//                doc.insertString(doc.getLength(), "Caused by: ", styleException);
//                imprimir(cause);
//            }
//            Log.setCaretPosition(doc.getLength());
//        } catch (BadLocationException ignorada) {
//        }
//    }
}